
//let delay = 3000;
let shareDelayTimeTick=3000;
let isVideo=false;
let shareStartTimeTick=0;
let shareSuccessCb=function(){};
let successCb=function(){};
let failCb=function(){};
let stopCb=function(){};
let videoAd=null;
//let shareFailCb=function(){};

var WechatShare = {
    
    // offEvent(){
    //     if(!CC_WECHATGAME){
    //         return;
    //     }
    //     this._onshow && wx.offShow(this._onshow);
    //     this._onshow = null;
    // },


    // share(t,p,cb){
    //     this.offEvent();
    //     this.lastTime = Date.now();
    //     this._onshow = function(){
    //         if(global.Data.isCheck){
    //             cb(true)
    //         } else {
    //             var dt = Date.now() - this.lastTime;
    //             console.log('分享间隔:',dt);
    //             if(dt < delay){
    //                 global.EJMgr.showToast('分享失败，请分享到不同的群试试');
    //             } else {
    //                 cb(true)
    //             }
    //         }
    //         this.offEvent();
    //     }.bind(this)
    //     wx.onShow(this._onshow);
    //     global.Platform.share(t,p)
    // },
    share(options={
        imageUrlId:'',
        imageUrl:'',
        successCb:function(){}
    }){
        let args={
            title:'[新消息]还记得记忆中的雷电吗，快来跟我一起爽快射击吧。',
            imageUrlId:'Jq7c00s0RG-0pGKt_gpNvw',
            imageUrl:'https://oss.073c.com/planewar/fengxiangtu2.png',
        }
        Object.assign(args,options);
        shareStartTimeTick=new Date().getTime();
        if(!CC_WECHATGAME){
            return options.successCb && options.successCb()
        }
        shareSuccessCb=options.successCb;
        wx.shareAppMessage(args);
        cc.game.once(cc.game.EVENT_SHOW, this._shareOnShow);
    },
    videoOrShare(options={
        adUnitId:'',
        stopCb:function(){},
        successCb:function(){}
    }){
        options.failCb=()=>{
            this.share(options);
        };
        this.video(options);
    },
    _onError(e){
        cc.game.resume();
        isVideo=false;
        failCb();
    },
    _onClose(res){
        cc.log('res',res)
        if(!res){
            global.ErrorLog.upload('videoClose','视频关闭时没有res返回为空')
        }
        isVideo=false;
        cc.game.resume();
        if(res.isEnded){
            global.Http.get('/game/gameUser','channelEvent.add',{eventName:"观看视频成功"},function(err,res){})
            global.Data.isNewUser && global.Http.get('/game/gameUser','channelEvent.add',{eventName:"新用户观看视频成功"},function(err,res){})

            successCb();
        }else{
            stopCb();
        }
    },
    video(options={
        adUnitId:'',
        stopCb:function(){},
        failCb:function(){},
        successCb:function(){}
    }){
        cc.log('来到了视频播放|！');
        if(isVideo){
            return;
        }
        if(!CC_WECHATGAME){
            return options.successCb && options.successCb()
        }
        isVideo=true;
        failCb=options.failCb;
        stopCb=options.stopCb;
        successCb=options.successCb;
        if(!!videoAd){
            videoAd.offError(this._onError);
            videoAd.offClose(this._onClose);
        }
        videoAd = wx.createRewardedVideoAd({ adUnitId: options.adUnitId });
        videoAd.load().then(() =>{
            cc.log('广告准备完成');
            videoAd.show().then(()=>{
                cc.log('广告开始显示');
                cc.game.pause();
            }).catch(err=>{
                isVideo=false;
                options.failCb();
            });
        });
        videoAd.onError(this._onError);
        videoAd.onClose(this._onClose);
    },
    _shareOnShow(){
        let now=new Date().getTime();
        if(now-shareStartTimeTick>=shareDelayTimeTick){
            shareSuccessCb();
        }else{
            global.EJMgr.showToast('分享失败，请分享到不同的群试试');
        }
    }
}

module.exports = WechatShare
